1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.solr.internal.csv;
18  
19  import java.io.IOException;
20  
21  import junit.framework.TestCase;
22  
23  /**
24   * CSVUtilsTest
25   */
26  public class CSVUtilsTest extends TestCase {
27    
28    // ======================================================
29    //   static parser tests
30    // ======================================================
31    public void testParse1() throws IOException {
32        String[][] data = CSVUtils.parse("abc\ndef");
33        assertEquals(2, data.length);
34        assertEquals(1, data[0].length);
35        assertEquals(1, data[1].length);
36        assertEquals("abc", data[0][0]);
37        assertEquals("def", data[1][0]);
38      }
39  
40      public void testParse2() throws IOException {
41        String[][] data = CSVUtils.parse("abc,def,\"ghi,jkl\"\ndef");
42        assertEquals(2, data.length);
43        assertEquals(3, data[0].length);
44        assertEquals(1, data[1].length);
45        assertEquals("abc", data[0][0]);
46        assertEquals("def", data[0][1]);
47        assertEquals("ghi,jkl", data[0][2]);
48        assertEquals("def", data[1][0]);
49      }
50  
51      public void testParse3() throws IOException {
52        String[][] data = CSVUtils.parse("abc,\"def\nghi\"\njkl");
53        assertEquals(2, data.length);
54        assertEquals(2, data[0].length);
55        assertEquals(1, data[1].length);
56        assertEquals("abc", data[0][0]);
57        assertEquals("def\nghi", data[0][1]);
58        assertEquals("jkl", data[1][0]);
59      }
60  
61      public void testParse4() throws IOException {
62        String[][] data = CSVUtils.parse("abc,\"def\\\\nghi\"\njkl");
63        assertEquals(2, data.length);
64        assertEquals(2, data[0].length);
65        assertEquals(1, data[1].length);
66        assertEquals("abc", data[0][0]);
67        // an escape char in quotes only escapes a delimiter, not itself
68        assertEquals("def\\\\nghi", data[0][1]);
69        assertEquals("jkl", data[1][0]);
70      }
71  
72      public void testParse5() throws IOException {
73        String[][] data = CSVUtils.parse("abc,def\\nghi\njkl");
74        assertEquals(2, data.length);
75        assertEquals(2, data[0].length);
76        assertEquals(1, data[1].length);
77        assertEquals("abc", data[0][0]);
78        assertEquals("def\\nghi", data[0][1]);
79        assertEquals("jkl", data[1][0]);
80      }
81      
82      public void testParse6() throws IOException {
83        String[][] data = CSVUtils.parse("");
84        // default strategy is CSV, which ignores empty lines
85        assertEquals(0, data.length);
86      }
87      
88      public void testParse7() throws IOException {
89        boolean io = false;
90        try {
91          CSVUtils.parse(null);
92        } catch (IllegalArgumentException e) {
93          io = true;
94        }
95        assertTrue(io);
96      }
97      
98      public void testParseLine1() throws IOException {
99        String[] data = CSVUtils.parseLine("abc,def,ghi");
100       assertEquals(3, data.length);
101       assertEquals("abc", data[0]);
102       assertEquals("def", data[1]);
103       assertEquals("ghi", data[2]);
104     }
105 
106     public void testParseLine2() throws IOException {
107       String[] data = CSVUtils.parseLine("abc,def,ghi\n");
108       assertEquals(3, data.length);
109       assertEquals("abc", data[0]);
110       assertEquals("def", data[1]);
111       assertEquals("ghi", data[2]);
112     }
113 
114     public void testParseLine3() throws IOException {
115       String[] data = CSVUtils.parseLine("abc,\"def,ghi\"");
116       assertEquals(2, data.length);
117       assertEquals("abc", data[0]);
118       assertEquals("def,ghi", data[1]);
119     }
120 
121     public void testParseLine4() throws IOException {
122       String[] data = CSVUtils.parseLine("abc,\"def\nghi\"");
123       assertEquals(2, data.length);
124       assertEquals("abc", data[0]);
125       assertEquals("def\nghi", data[1]);
126     }
127     
128     public void testParseLine5() throws IOException {
129       String[] data = CSVUtils.parseLine("");
130       assertEquals(0, data.length);
131       // assertEquals("", data[0]);
132     }
133     
134     public void testParseLine6() throws IOException {
135       boolean io = false;
136       try {
137         CSVUtils.parseLine(null);
138       } catch (IllegalArgumentException e) {
139         io = true;
140       }
141       assertTrue(io);
142     }
143     
144     public void testParseLine7() throws IOException {
145       String[] res = CSVUtils.parseLine("");
146       assertNotNull(res);
147       assertEquals(0, res.length);  
148     }
149       
150 }